/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-08-21
 * Time: 22:28
 */
//https://leetcode.cn/problems/find-peak-element/
public class Test {
    public static int findPeakElement(int[] arr) {
        int size=arr.length;
        if(size==1){
            return 0;
        }
        if(arr[0]>arr[1]){
            return 0;
        }
        if(arr[size-1]>arr[size-2]){
            return size-1;
        }
        int p1=1,p2=size-2;
        int p=(p1+p2)/2;
        while(p1<=p2){
            if(arr[p-1]<arr[p]&&arr[p]>arr[p+1]){
                return p;
            }
            if(arr[p]>arr[p-1]){
                p1=p+1;
            }
            else{
                p2=p-1;
            }
            p=(p1+p2)/2;
        }
        return p;
    }

    public static void main(String[] args) {
        int[] arr={1,2,1,3,5,6,4};
        System.out.println(findPeakElement(arr));
    }
}
